package vn.aptech.scrumgroup.model;

import vn.aptech.scrumgroup.util.DrawUtil;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import demo.axitdn.physic.Unit;

public class PSIModel extends AbstractBaseModel{
	private int width;
	private int height;
	private Point startPoint;
	private int centerX;
	private int centerY;
//	private float more;
	private float charWidth;
	private Path path;
	float boundLeft;
	float boundRight;
	float boundTop;
	float boundBottom;
	
	public PSIModel(Point startPoint, int width, int height, float charWidth, int backColor, int foreColor){
		super(backColor, foreColor);
		this.width = width;
		this.height = height;
		this.startPoint = startPoint;
		this.charWidth = charWidth;	
	}
	@Override
	public void onDraw(Canvas c) {
		// TODO Auto-generated method stub
		
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		ColorMatrix cm = new ColorMatrix();
		int brightness = 120;
		if (isBrilliant){ 
			cm.set(new float[]{1,0,0,0,brightness,
		    		0,1,0,0,brightness,
		    		0,0,1,0,brightness,
		    		0,0,0,1,0
		    		});
			paint.setColorFilter(new ColorMatrixColorFilter(cm));
		}else {
			paint.setColorFilter(null);
		}//end else
		
		//draw psi model
		paint.setColor(backColor);
		paint.setStyle(Style.FILL);
		
		path = new Path();
		path.moveTo(startPoint.x, startPoint.y);
		path.lineTo(startPoint.x, startPoint.y + height);
		path.lineTo(startPoint.x - width, startPoint.y + height);
		path.lineTo(startPoint.x - width, startPoint.y);
		path.lineTo(startPoint.x - width + width / 5, startPoint.y - height / 3);
		path.lineTo(startPoint.x + width / 5, startPoint.y - height / 3);
		path.lineTo(startPoint.x + width / 5, startPoint.y - height / 3 + height);
		path.lineTo(startPoint.x, startPoint.y + height);
		c.drawPath(path, paint);
		
		//draw psi model's line inside
		//paint.setColor(Color.WHITE);
		paint.setAlpha(0);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(Unit.i);
		paint.setAntiAlias(true);
		
		path = new Path();
		path.moveTo(startPoint.x, startPoint.y);
		path.lineTo(startPoint.x - width + Unit.i, startPoint.y);
		path.moveTo(startPoint.x, startPoint.y);
		path.lineTo(startPoint.x + width / 5 - Unit.i, startPoint.y - height / 3 + Unit.j);
		path.moveTo(startPoint.x, startPoint.y);
		path.lineTo(startPoint.x, startPoint.y + height - Unit.j);
		c.drawPath(path, paint);
		
		//draw text
		paint.setAlpha(255);
		paint.setColor(foreColor);
		paint.setTextAlign(Align.CENTER);
		centerX = startPoint.x - width / 2;
		centerY = startPoint.y + (int)(height * 1.5f);
		
		boundTop = startPoint.y - height / 3;
		boundBottom = centerY + height * 2 + charWidth;
		boundRight = startPoint.x + width / 5;
		boundLeft = centerX - paint.measureText("Shippable Produce") / 2;
		
		paint.setTextSize(DrawUtil.calculateTextSize(new String[]{"Potentially", "Shippable Produce", "Increment"}, boundRight-boundLeft, paint, 3));
		drawText(c, "Potentially", centerX, centerY, paint, charWidth);
		drawText(c, "Shippable Produce", centerX, centerY + height, paint, charWidth);
		drawText(c, "Increment", centerX, centerY + height * 2, paint, charWidth);
	}

	private void drawText(Canvas canvas, String text, float x, float y, Paint paint, float charWidth)
	{
//		paint.setStyle(Style.FILL_AND_STROKE);
//		if(charWidth > 0){s
//			paint.setStrokeWidth(charWidth);
//		}
//		if(text.equals("Shippable Produce")){
//			float widthText = paint.measureText(text);
//			
//		}
//		paint.setTextSize(height * 0.8f);
		paint.setAntiAlias(true);
		Rect r = new Rect();
		paint.getTextBounds(text, 0, text.length(), r);
		canvas.drawText(text, x, y + r.height()/2, paint);
	}
	
	@Override
	public void onUpdate(double t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isPointOf(float x, float y) {
		// TODO Auto-generated method stub
		if(x <= boundRight && x >= boundLeft)
			if(y <= boundBottom && y >= boundTop)
				return true;
		return false;
	}
	@Override
	public void changeColor(int color) {
		// TODO Auto-generated method stub
		
	}
}

